﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Northwinddemo.DAL.EntityClasses;
using Northwinddemo.DAL.HelperClasses;
using System.Web.Mvc;
using Northwinddemo.BusinessLayer.Repository.Abstract;

namespace Northwinddemo.BusinessLayer
{
    public static class NorthwinddemoHelper
    {
        public static void SetSuppliersToViewData(ViewDataDictionary viewData,
            ISuppliersRepository suppliersRepository)
        {
            viewData["suppliers"] = from s in suppliersRepository.GetAll()
                                    select new { Key = s.SupplierId, Value = GetSupplierDescription(s) };
        }

        public static void SetSupplierToViewData(int supplierId, 
            ViewDataDictionary viewData,
            ISuppliersRepository suppliersRepository)
        {
            var supplier = suppliersRepository.GetById(supplierId);
            viewData["supplier"] = GetSupplierDescription(supplier);
        }

        public static void SetCategoriesToViewData(ViewDataDictionary viewData,
            ICategoriesRepository categoriesRepository)
        {
            viewData["categories"] = from c in categoriesRepository.GetAll()
                                     select new { Key = c.CategoryId, Value = GetCategoryDescription(c) };
        }

        public static void SetCategoryToViewData(int categoryId,
            ViewDataDictionary viewData,
            ICategoriesRepository categoriesRepository)
        {
            var category = categoriesRepository.GetById(categoryId);
            viewData["category"] = GetCategoryDescription(category);
        }

        public static string GetSupplierDescription(SuppliersEntity supplier)
        {
            return supplier.CompanyName + " (" + supplier.City + ", " + supplier.Country + ")";
        }

        public static string GetCategoryDescription(CategoriesEntity category)
        {
            return category.CategoryName;
        }
    }
}
